package net.qiqb.core.domain.annotation;

import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 可拓展的聚合根
 *
 * @param <AGID>
 */
@Slf4j
public abstract class ExpandableAggregateRoot<AGID extends Serializable> extends AbstractAggregateRoot<AGID> {

    protected ExpandableAggregateRoot(AGID id) {
        super(id);
    }

    protected HashMap<String, Object> extraDomainObj = new HashMap<>();

    public List<Object> getDomainObjs() {
        return Arrays.asList(extraDomainObj.values().toArray());
    }

    public <T> T getAttachedObject(Class<T> entityClass) {
        final Object obj = extraDomainObj.get(entityClass.getName());
        if (obj != null) {
            return (T) obj;
        }
        return null;
    }

    /**
     * 附加对象
     *
     * @param entity
     */
    public void attachObject(Object entity) {
        extraDomainObj.put(entity.getClass().getName(), entity);
    }


}
